<?php
/**
 * 2018-07-20
 * 
 * 刘家腾
 * 
 * class:FeedbackController
 */
namespace App\Http\Controllers;

use App\Common\Base;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
use App\Service\DataReturnService as DRS;
use Illuminate\Support\Facades\Validator;

class FeedbackController extends Controller
{
    public function __construct() { }
    
   
    /**
     * 2018-7-20
     * 
     * 刘家腾
     * 
     * 常见问题 列表
     * 
     */
    public function post(Request $R)
    {
        $postData  = $R->all();
        if (! $R->filled('token')) {
            return DRS::returnError(3000,'缺少核心参数：token');
        }
        $userInfo = Base::getUserByToken($postData['token']);
        if($userInfo['code'] != '666'){
            return $userInfo;
        }
        if (! $R->filled('business_id')) {
            return DRS::returnError(3000,'缺少核心参数：business_id');
        }
        if (! $R->filled('type')) {
           return DRS::returnError(3000,'缺少核心参数：type');
        }

        $user = $userInfo['data'];
        $data['user_id']=$user['id'];
        $data['business_id']=$user['business_id'];
        $data['type']=$postData['type'];

        $data['create_time']=date("Y-m-d H:i:s");

        if ($R->filled('contact')) {
            $data['contact']=$postData['contact'];
        }
        if ($R->filled('content')) {
            $data['content']=$postData['content'];
        }

        if ( empty($data['contact']) && empty($data['content'])) {
           return DRS::returnError(3000,'内容和联系方式不能同时为空！');
        }
        $feedback_id = DB::table('feedback')->insertGetId($data);

        $image_data=[];
        if ($R->filled('images') && $feedback_id) {
            foreach ($postData['images'] as $key => $value) {
                $image_data[]=['feedback_id' => $feedback_id, 'pic_url' => $value,'create_time'=>$data['create_time']];
            }
            DB::table('feedback_pics')->insert($image_data);
        }
        if ($feedback_id) {
            return DRS::returnOk($feedback_id,'提交成功,感谢您的反馈');
        }
        
    }


     /**
     *
     * @param  Request  $request
     * @return Response
     */
    public function update(Request $request)
    {
        $images=Request::file('imgfile'); //1、使用laravel 自带的request类来获取一下文
        $filedir="upload/article-img/"; //2、定义图片上传路径
        $imagesName=$images->getClientOriginalName(); //3、获取上传图片的文件名
        $extension = $images -> getClientOriginalExtension(); //4、获取上传图片的后缀名
        $newImagesName=md5(time()).random_int(5,5).".".$extension; //5、重新命名上传文件名字
        $images->move($filedir,$newImagesName); //6、使用move方法移动文件.
    
        return $filedir.$newImagesName;
    }
    


}